﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BitcoinCommon;
using BitcoinMarket.Data;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace BitcoinMarket.Market
{
    public class BitstampPrice
    {
        MainInfo Comm;
        public double LastPrice;
        public double HighPrice;
        public double LowPrice;
        public double AskPrice;
        public double BidPrice;
        public double Volume;
        
        public BitstampPrice(MainInfo objComm)
        {
            Comm = objComm;
        }

        public void RetrivedData()
        {
            string strURL;
            string strData;

            strURL = StringURL.URL_BITSTAMP_LAST_CONTRACT;

            strData = Comm.RequestData(strURL, "");

            if (strData.Length == 0)
                return;

            ConvertToData(strData);
        }

        private void ConvertToData(string strData)
        {
            string strLastPrice;
            string strHighPrice;
            string strLowPrice;
            string strVolume;
            string strAskPrice;
            string strBidPrice;

            if (strData.Length == 0)
                return;

            try
            {
                JObject objItem = JObject.Parse(strData);

                strHighPrice = objItem["high"].ToString();
                strLowPrice = objItem["low"].ToString();
                strLastPrice = objItem["last"].ToString();
                strVolume = objItem["volume"].ToString();
                strAskPrice = objItem["ask"].ToString();
                strBidPrice = objItem["bid"].ToString();

                LastPrice = Double.Parse(strLastPrice);
                HighPrice = Double.Parse(strHighPrice);
                LowPrice = Double.Parse(strLowPrice);
                Volume = Double.Parse(strVolume);
                AskPrice = Double.Parse(strAskPrice);
                BidPrice = Double.Parse(strBidPrice);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }
    }
}
